<template>
  <div class="details-container app-container" v-loading="listLoading">
    <div class="detail-title">
      <div>用户基础信息</div>
      <el-button type="primary" size="small" @click="changeUserInfo">修改</el-button>
    </div>
    <div class="detail-content">
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">用户名称:</div>
          <div><el-input v-model="userDetail.nick_name" /></div>
        </el-col>
        <el-col :span="12">
          <div class="left-text">用户ID: </div>
          <div>{{ userDetail.id_public_user_info }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">所属门店:</div>
          <div>{{ userDetail.agent_name }}</div>
        </el-col>
        <el-col :span="12">
          <div class="left-text">用户类型: </div>
          <div>
            <el-radio-group v-model="userDetail.user_type">
              <el-radio :label="0">普通员工</el-radio>
              <el-radio :label="111">普通店员</el-radio>
              <el-radio :label="112">店长</el-radio>
              <el-radio :label="113">运营总监</el-radio>
            </el-radio-group>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">性别:</div>
          <div>{{ userDetail.user_sex === 1 ? '男' : (userDetail.user_sex === 2 ? '女' : '不详') }}</div>
        </el-col>
        <el-col :span="12">
          <div class="left-text">注册时间: </div>
          <div>{{ userDetail.time_register }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">手机号:</div>
          <div><el-input v-model="userDetail.user_phone" /></div>
        </el-col>
        <el-col :span="12">
          <div class="left-text">地址: </div>
          <div>{{ userDetail.add_desc || '-' }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">最后消费时间:</div>
          <div>{{ userDetail.time_update }}</div>
        </el-col>
        <el-col :span="12">
          <div class="left-text">最后登录时间: </div>
          <div>{{ userDetail.login_time }}</div>
        </el-col>
      </el-row>
      <el-row :gutter="80">
        <el-col :span="12">
          <div class="left-text">修改密码:</div>
          <div><el-input v-model="password" /></div>
        </el-col>
      </el-row>
    </div>
    <div class="other-content">
      <div class="item-content">
        <div>
          <div>账户余额</div>
          <div class="number-text">{{ otherInfo.left_money }}元</div>
        </div>

        <div><el-button  @click="dialogVisible = true">余额充值</el-button></div>
      </div>
      <div class="item-content">
        <div>
          <div>积分余额</div>
          <div class="number-text">{{ otherInfo.left_score }}分</div>
        </div>
        <div><el-button @click="dialogVisible2 = true"> 积分充值</el-button></div>
      </div>
      <div class="item-content">
        <div>
          <div>优惠券</div>
          <div class="number-text">已配置的优惠券 {{ totalNum }} 条，已上线 {{ onlineNum }} 张</div>
        </div>
        <div><el-button @click="dialogVisible3 = true">  赠送优惠券</el-button></div>
      </div>
    </div>

    <el-dialog
      title="余额详情"
      :visible.sync="dialogVisible"
      width="610px"
      :rules="rules"
    >
      <div class="title_one">用户余额详情：{{otherInfo.left_money}}</div>
      <el-form :inline="true" :model="rechargeConsume1" class="demo-form-inline" :rules="rules">
        <el-form-item label="用户余额充值：" prop="consume_price">
          <el-input v-model="rechargeConsume1.consume_price" placeholder="请输入充值金额" clearable></el-input>
        </el-form-item>

        <el-form-item label="用户备注信息：" prop="recharge_rules" >
          <el-input v-model="rechargeConsume1.consume_remark" placeholder="请输入备注信息" type="textarea" ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="rechargeConsume" >确认充值</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="积分详情"
      :visible.sync="dialogVisible2"
      width="610px"
      :rules="rules"
    >
      <div class="title_one">用户积分详情：{{otherInfo.left_score}}</div>
      <el-form :inline="true" :model="rechargeScore1" class="demo-form-inline" :rules="rules">
        <el-form-item label="用户积分充值：" prop="consume_score"  >
          <el-input v-model="rechargeScore1.consume_score" placeholder="请输入充值积分" clearable></el-input>
        </el-form-item>

          <el-form-item label="用户备注信息：" prop="recharge_rules" >
            <el-input v-model="rechargeScore1.score_remark" placeholder="请输入备注信息" type="textarea" ></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="rechargeScore">确认充值</el-button>
  </span>
    </el-dialog>

    <el-dialog
      title="赠送优惠券"
      :visible.sync="dialogVisible3"
      width="610px"
      :rules="rules"
    >
      <div class="title_one">已上线优惠券 {{ onlineNum }} 张</div>
      <el-form :inline="true" :model="rechargeScore1" class="demo-form-inline" :rules="rules">
        <el-form-item label="赠送用户优惠券：" prop="consume_score"  >
          <el-select v-model="value" @change="getKey" placeholder="请选择" clearable>
            <el-option
              v-for="item in public_score_list"
              :key="item.id_public_rule_4_score"
              :label="item.score_rule_remark"
              :value="item.score_rule_remark">
            </el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="备注信息：" prop="recharge_rules" style="margin-left: 5px;">
            <el-input v-model="rechargeScore1.score_remark" placeholder="请输入备注信息" type="textarea" ></el-input>
          </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible3 = false">取 消</el-button>
    <el-button type="primary" @click="rechargeCoupon">确认赠送</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves' // waves directive
import { orderList, userRecharge } from '@/api/order'
import {couponList} from '@/api/coupon'

export default {
  name: 'UserDetails',
  directives: { waves },
  data() {
    return {
      listLoading: true,
      dialogVisible: false,
      dialogVisible2: false,
      dialogVisible3: false,
      rules:{
        consume_price: [
          { required: true, message: '请输入充值金额', trigger: 'blur' }
        ],
        consume_score: [
          { required: true, message: '请输入充值积分', trigger: 'blur' }
        ]
      },
      userDetail: {},
      otherInfo: {},
      password: '',
      detailQuery: {
        invoke_method: 'public_page_4m_user_4_balance',
        buyer_id: null
      },

      changeQuery: {
        invoke_method: 'change_user_info',
        nick_name: null,
        user_phone: null,
        user_type: null,
        pre_db_para_id_public_user_info: null,
        password_user: null
      },

      rechargeConsume1: {
        invoke_method: 'public_page_4m_recharge_user_consume',
        consume_price: null,
        consume_remark: null,
        pre_cmd_para_empower_code: '123',
      },

      rechargeScore1: {
        invoke_method: 'public_page_4m_recharge_user_score',
        consume_score: null,
        score_remark: null,
        pre_cmd_para_empower_code: '123',
      },

      rechargeCoupon1: {
        invoke_method: 'public_page_4m_recharge_user_quan',
        pre_cmd_para_empower_code: '123',
        id_public_rule_4_score: null,
      },

      listQuery: {
        invoke_method: 'load_score_config',
        type_rule_4_score: 106
      },

      Coupon: [],
      public_score_list: [],
      value: '',
      totalNum: 0,
      onlineNum: 0,
    }
  },
  mounted() {
    this.detailQuery.buyer_id = this.$route.params.id
    this.changeQuery.pre_db_para_id_public_user_info = this.$route.params.id
    this.getList()
    this.getCoupon()
  },

  methods: {
    /*
    * 获取页面数据
    * */
    getList() {
      this.listLoading = true
      orderList(this.Qs.stringify(this.detailQuery)).then(response => {
        this.userDetail = response.public_user_info
        this.otherInfo = response
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },

      /*优惠券数据*/
    getCoupon() {
      couponList(this.Qs.stringify(this.listQuery)).then(response => {
        console.log(response)
        this.totalNum = response.length_4_list
        this.public_score_list = response.public_score_list
        response.public_score_list.forEach(item => {
          if (item.is_online === 0) {
            this.onlineNum += 1
          }
        })
      })
    },
    /*
    * 修改用户信息
    * */
    changeUserInfo() {
      this.changeQuery.nick_name = this.userDetail.nick_name
      this.changeQuery.user_phone = this.userDetail.user_phone
      this.changeQuery.id_public_user_info = this.userDetail.id_public_user_info
      this.changeQuery.password_user = this.password
      this.changeQuery.user_type = this.userDetail.user_type
      orderList(this.Qs.stringify(this.changeQuery)).then(res => {
        this.$notify({
          title: 'Success',
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },

    /*充值余额*/
    rechargeConsume(){
      userRecharge(this.Qs.stringify({
        invoke_method: 'public_page_4m_recharge_user_consume',
        buyer_id: this.$route.query.user_id,
        consume_price: this.rechargeConsume1.consume_price,
        consume_remark: this.rechargeConsume1.consume_remark,
        consume_type: this.rechargeConsume1.consume_type,
        pre_cmd_para_empower_code: this.rechargeConsume1.pre_cmd_para_empower_code,
      })).then(() =>{
        this.dialogVisible = false
        this.$notify({
          title: 'Success',
          message: '充值成功',
          type: 'success',
          duration: 2000
        })
      })
      this.getList()
      },

    /*充值积分*/
    rechargeScore(){
      userRecharge(this.Qs.stringify({
        invoke_method: 'public_page_4m_recharge_user_score',
        buyer_id: this.$route.query.user_id,
        consume_score: this.rechargeScore1.consume_score,
        score_remark: this.rechargeScore1.score_remark,
        consume_type: this.rechargeScore1.consume_type,
        pre_cmd_para_empower_code: this.rechargeScore1.pre_cmd_para_empower_code,
      })).then(() =>{
          this.dialogVisible2 = false
          this.$notify({
            title: 'Success',
            message: '充值成功',
            type: 'success',
            duration: 2000
            })
        })
      this.getList()
      },

    /*赠送优惠券*/
    rechargeCoupon(){
      userRecharge(this.Qs.stringify({
        invoke_method: 'public_page_4m_recharge_user_quan',
        id_public_user_info : this.$route.query.user_id,
        pre_cmd_para_empower_code: this.rechargeCoupon1.pre_cmd_para_empower_code,
        id_public_rule_4_score: this.rechargeCoupon1.id_public_rule_4_score,
      })).then(() =>{
        this.dialogVisible3 = false
        this.$notify({
          title: 'Success',
          message: '赠送成功',
          type: 'success',
          duration: 2000
        })
      })
      this.getList()
    },

    getKey(value){this.public_score_list.forEach(item =>{if(item.score_rule_remark===value){console.log(item);
      this.rechargeCoupon1.id_public_rule_4_score = item.id_public_rule_4_score
    }})}
    }
}

</script>

<style lang="scss" scoped>
@import "../../styles/variables";

.detail-title {
  border-bottom: 1px solid #e3e3e3;
  padding: 16px 0;
  font-size: 16px;
  font-weight: bold;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.detail-content {
  padding: 24px 24px 0;
  .el-row {
    margin-bottom: 36px;
    font-size: 14px;
    display: flex;

    .el-col {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      .left-text {
        color: #2d2f33;
      }
      .el-input {
        ::v-deep {
          .el-input__inner {
            border-radius: 0;
            border: none;
            border-bottom: 1px solid $tiffany;
            padding-left: 0;
            text-align: right;
          }
        }
      }
    }
  }
}

.other-content {
  .item-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    border: 1px solid #e3e3e3;
    border-bottom: none;
    &:last-child {
      border-bottom: 1px solid #e3e3e3;
    }

    .number-text {
      color: #409EFF;
      font-weight: bold;
      margin-top: 12px;
    }

    .el-button {
      background-color: transparent;
      border: none;
      color: #409EFF;
    }
  }
}

.title_one{
  margin-bottom: 10px;
}

  .label{
    font-weight: 400;
  }
</style>
